/* 
辅助F12，手动快速搜索Web page内容，url已写死，针对非全硕士内容检索
@命令行
node web_search.js 搜索关键字
*/
const Koa = require('koa2'),
    Router = require('koa-router'),
    cheerio = require('cheerio'),
    path = require('path'),
    superagent = require('superagent');
const fs = require("fs");

require('superagent-charset')(superagent);

const app = new Koa(),
      router = new Router();

const argvs = process.argv.slice(2);
const params = {
    keyword: argvs[0] || '非全',
    host:'https://admission.ucas.ac.cn',
    urls: [
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8000912023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8001012023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8001412023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8001712023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8001812023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8002212023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8002312023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8002412023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8002612023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8003012023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8007312023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8008612023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8009212023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8009312023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8013212023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8013612023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8013812023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8013912023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8014012023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8014212023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8014712023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8015012023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8015112023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8015912023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8017712023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8018412023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8018512023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8018612023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8018812023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8019312023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8019412023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8019512023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8020212023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8020412023",
        "/info/ZhaoshengDanweiDetail/9e780c52-baf5-4020-b453-bc4510579559/8020612023"
    ],
}
const getPage = (url) => {
    return new Promise((ok, fail) => {
        superagent
            .get(url)
            .set({
                "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
            })
            .charset('utf-8').buffer(true).then(sres => {
                if (sres) {
                    // ok(sres.text)
                    // const $ = cheerio.load(sres.text, {
                    //     decodeEntities: false
                    // });
                    // const charset = ($('head meta[http-equiv="Content-Type"]')[0].attribs.content);
                    // // console.log(sres.text)
                    // ok('ok')
                    ok(sres.text)
                }
            }).catch((err) => {
                ok('in err ' + JSON.stringify(err));
                if (err) {
                    console.log('catch err')
                }
            });
    });
}
function find(temp1) {
    var list = [];
    $(temp1).find('tr').each(function () { 
        var tr = $(this), tds = tr.find('td');
        if ($(tds[1]).html().indexOf('085400') > -1) {
            list.push($(tds[2]).find('a').attr('href'));
        }
    })
    return list;
}
const searchPage = async (url) => {
    /* 
    @url        string / array
    */
    if (url instanceof Array) {
        searchPage.urls = url;
        searchPage.index = 0;
        console.log('-------- start 遍历 --------')
        searchPage();
        return;
    }
    if (!url) {
        url = (searchPage.urls || [])[searchPage.index];
        if (url) {
            searchPage.index += 1;
            searchPage(params.host + url);
        } else {
            searchPage.urls = [];
            searchPage.index = -1;
            console.log('-------- end --------')
        }
    } else {
        // console.log(params.keyword, url);
        const text = await getPage(url);
        // console.log(text)
        if (text.indexOf(params.keyword) > -1) {
            console.log('有：',url)
        }
        searchPage();
    }
}
// searchPage(params.host + params.urls[params.urls.length - 1]);
searchPage(params.urls);
